﻿using System;
using cfg.Myskill;
using QFramework;
using Unity.VisualScripting;
using UnityEngine;

public class CrystalCloneController:CloneController
{
    private  bool isExplosion;
    private bool isClone;
    private bool isNbomb;
    private float moveSpeed;
    private float explosionFroce;
    private Crystal info;

    protected override void Awake()
    {
        base.Awake();
        cd.OnTriggerEnter2DEvent(OnTriggerEnter2D);
    }

    public override void Init(CloneBase _info, Vector2 pos, int faceDir)
    {
        base.Init(_info, pos, faceDir);
        info = (Crystal)_info;
        Play("crystal_idle");
        controller = PlayerManager.Instance.controller;
        Reset();
        //监听主动销毁
        EventCenter.Instance.AddEventListener(E_EventType.Crystal_Active_Destroy, CrystalDestroy);
        
        //监听水晶触发
        EventCenter.Instance.AddEventListener(E_EventType.Crystal_Trigger, CrystalTrigger);
    }

    public override void Reset()
    {
        isExplosion = false;
        isClone = false;
        isNbomb = false;
        cd.enabled = false;//默认关闭碰撞
        explosionFroce = 0;
        transform.localScale = Vector3.one;
    }

    #region 事件
    private void CrystalTrigger()
    {
        if (controller!=null)
        {
            // 创建一个临时向量来存储当前对象的位置
            Vector2 temp = transform.position;

            // 将控制器的位置赋给当前对象
            transform.position = controller.transform.position;

            // 将临时存储的位置赋给控制器
            controller.transform.position = temp;
            
            //是否创造残影攻击
            if (isClone)
            {
                SkillManager.Instance.CreateCloneAttackWithCoroutine
                    (transform.position,faceDir,info.SkillId,true);
            }
        }
        //是否爆炸
        if (isExplosion||isNbomb)
        {
            Play("crystal_explosion");
        }
        else
        {
            if (!isClone)//分身水晶不会触发后消失，只会时间到了就销毁
            {
                DestroySelf();
                
            }
        }
    }
    
    //销毁
    private void CrystalDestroy()
    {
        //是否爆炸
        if (isExplosion||isNbomb)
        {
            Play("crystal_explosion");
        }
        else
        {
            DestroySelf();
        }
    }
    

    #endregion
    
    #region Animator
    private void TriggerExplosion()
    {
        if (isNbomb)
        {
            cd.enabled = false;//关闭碰撞
            transform.localScale = Vector3.one*5;
            FindEnemies(4f);
        }
        else
        {
            transform.localScale = Vector3.one*3;
            FindEnemies(2f);
        }
    }

    private void TriggerDestroy()
    {
        DestroySelf();
    }

    protected override void DestroySelf()
    {
        //完成销毁
        EventCenter.Instance.EventTrigger(E_EventType.Crystal_Finish_Destroy);
        EventCenter.Instance.RemoveEventListener(E_EventType.Crystal_Active_Destroy, CrystalDestroy);
        EventCenter.Instance.RemoveEventListener(E_EventType.Crystal_Trigger, CrystalTrigger);
        base.DestroySelf();
    }

    private void FindEnemies(float radius)
    {
        var enemys = Physics2D.OverlapCircleAll(transform.position, radius, Setting.EnemyLayer);
        foreach (var enemy in enemys)
        {
            //计算一下方向
            var  dir = transform.position.x<enemy.transform.position.x?1:-1;
             var enemyController = enemy.GetComponent<EnemyStateController>();
             enemyController?.Damage(new DamgeInfo()
             {
                damage = controller.GetDamage()+info.SkillDamage,
                attacker = controller.characterBase,
                targeter = enemyController.characterBase,
                hitBackForce = new Vector2(dir*explosionFroce,explosionFroce),
                hitBackTime = 0.3f,
             });
        }
    }
    #endregion

    #region 条件
    public void CanExplosion()
    {
        explosionFroce = info.ExplosiveForce;
        isExplosion = true;
    }

    public void CanClone()
    {
        isClone = true;
    }

    public void CanNbomb()
    {
        explosionFroce = info.ExplosiveForce;
        moveSpeed = info.MoveSpeed;
        // 开启碰撞
        cd.enabled = true;
        isNbomb = true;
    }
    #endregion

    protected override void Update()
    {
        //base.Update();
        //Move
        if (moveSpeed>0)
        {
            rb.velocity = Vector2.right*faceDir * moveSpeed;
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        Debuger.Log("OnCollisionEnter2D"+other.transform.name);
        if (isNbomb)//爆炸
        {
            cd.enabled = false;
            Play("crystal_explosion");
        }
    }
}